package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MenuDao;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.SysUser;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;
    @Autowired
    RoleDao roleDao;
    @Autowired
    PermissionDao permissionDao;
    @Autowired
    MenuDao menuDao;

    @Override
    public SysUser findByusername(String username) {
       //根据用户名查用户信息
        SysUser sysUser = userDao.findByUsername(username);
        if (sysUser == null){
            return null;
        }
        //根据用户id查询角色对象
        Set<Role> roles = roleDao.findRoleListByUserId(sysUser.getId());
        if (roles == null || roles.size() == 0){
            return sysUser;
        }
        for (Role role : roles) {
           Set<Permission> permissions = permissionDao.findPermissionListByRoleId(role.getId());
            role.setPermissions(permissions);
        }
        sysUser.setRoles(roles);
        return sysUser;
    }

    @Override
    public Integer findIdByUsername(String username) {
        return userDao.findIdByUsername(username);
    }

    @Override
    public List<Map<String,Object>> showMenuByUserId(Integer userId) {
        //1.通过userid查roleid
        Integer roleId = userDao.findRoleIdByUserId(userId);
        //2.通过roleid查menu_id集合
        List<Integer> menuIds = roleDao.findMenuIdsByRoleId(roleId);
        //3.通过menuIds批量查menu的所有数据,问题在于封装数据的格式
        List<Menu> menuList = menuDao.queryMenuByMenuId(menuIds);
        //创建menu主菜单集合
        List<Map<String,Object>> zhuList = new ArrayList<>();
        //遍历集合
        for (Menu menu : menuList) {
            //判断是否为子菜单  null 则不是
            if(menu.getParentMenuId() == null){
                //创建map集合
                Map<String,Object> map = new LinkedHashMap<>();
                //放数据
                map.put("path",menu.getPath());
                map.put("title",menu.getName());
                map.put("icon",menu.getIcon());

                //创建子菜单集合
                List<Map<String,Object>> listzi = new ArrayList<>();
                for (Menu menu1 : menuList) {
                    Map<String,Object> mapzi = new LinkedHashMap<>();
                    //判断子菜单id与主菜单id是否相等
                    if (menu.getId() == menu1.getParentMenuId()){
                        mapzi.put("path",menu1.getPath());
                        mapzi.put("title",menu1.getName());
                        mapzi.put("linkUrl",menu1.getLinkUrl());
                        mapzi.put("children",new ArrayList<>());
                        listzi.add(mapzi);
                    }
                }
                map.put("children",listzi);
                zhuList.add(map);
            }
        }
        return zhuList;
    }
}
